﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Core.Framework
{
    public class EmailUtils
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toEmail">接收人</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">正文</param>
        /// <returns></returns>
        public static bool Send(string toEmail, string title, string content)
        {
            try
            {
                MailAddress from = new MailAddress(
                    AppSetting.GetValue("Email:User"),
                    AppSetting.GetValue("Email:Sign"),
                    Encoding.UTF8);

                MailAddress toing = new MailAddress(toEmail);

                MailMessage message = new MailMessage(from, toing);

                // 设置邮件主题 
                message.Subject = title;

                // 邮件内容使用HTML
                message.IsBodyHtml = true;

                // 设置邮件内容
                message.Body = content;

                message.BodyEncoding = Encoding.GetEncoding("utf-8");

                SmtpClient client = new SmtpClient(AppSetting.GetValue("Email:SMTP"));

                client.UseDefaultCredentials = false;

                client.EnableSsl = true;

                client.Credentials = new NetworkCredential(
                    AppSetting.GetValue("Email:LoginName"),
                    AppSetting.GetValue("Email:Password"));

                client.Send(message);

                return true;

            }
            catch (Exception e)
            {
                string info = e.Message;

                return false;
            }
        }
    }
}
